package com.qin.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qin.mall.dao.PmsProductCategoryDao;
import com.qin.mall.domain.PmsProductCategory;
import com.qin.mall.dto.PmsProductCategoryWithChildrenItem;
import com.qin.mall.dto.ProductCategoryParam;
import com.qin.mall.mapper.PmsProductCategoryMapper;
import com.qin.mall.service.PmsProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 覃剑
* @description 针对表【pms_product_category(产品分类)】的数据库操作Service实现
* @createDate 2022-11-06 15:50:53
*/
@Service
public class PmsProductCategoryServiceImpl extends ServiceImpl<PmsProductCategoryMapper, PmsProductCategory>
    implements PmsProductCategoryService {
    
    @Autowired
    private PmsProductCategoryMapper productCategoryMapper;
    
    @Autowired
    private PmsProductCategoryDao productCategoryDao;

    // TODO 创建商品分类
    @Override
    public int create(ProductCategoryParam productCategoryParam) {
        int insert = productCategoryMapper.insert(productCategoryParam);
        return insert;
    }

    //TODO 根据id修改商品 分类
    @Override
    public int update(Long id, ProductCategoryParam productCategoryParam) {
        QueryWrapper<PmsProductCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        int update = productCategoryMapper.update(productCategoryParam, wrapper);
        return update;
    }

    //TODO 分页获取商品分类
    @Override
    public Page<PmsProductCategory> getList(Long parentId,Integer pageNum,Integer pageSize) {
        QueryWrapper<PmsProductCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",parentId);
        Page<PmsProductCategory> page = new Page<>(pageNum, pageSize);
        Page<PmsProductCategory> pages = productCategoryMapper.selectPage(page, wrapper);
        return pages;
    }

    //TODO 根据id删除商品分类
    @Override
    public int delete(Long id) {
        int delete = productCategoryMapper.deleteById(id);
        return delete;
    }

    //TODO 根据ID获取商品分类
    @Override
    public PmsProductCategory getItem(Long id) {
        PmsProductCategory category = productCategoryMapper.selectById(id);
        return category;
    }

    //TODO 批量修改导航状态
    @Override
    public int updateNavStatus(List<Long> ids, Integer navStatus) {
        QueryWrapper<PmsProductCategory> wrapper = new QueryWrapper<>();
        PmsProductCategory category = new PmsProductCategory();
        //列id 在ids集合里面
        wrapper.in("id",ids);
        // 将navStatus 塞到category 里面
        category.setNavStatus(navStatus);
        int update = productCategoryMapper.update(category, wrapper);
        return update;
    }
    
    //TODO 批量修改显示状态
    @Override
    public int updateShowStatus(List<Long> ids, Integer showStatus) {
        QueryWrapper<PmsProductCategory> wrapper = new QueryWrapper<>();
        PmsProductCategory category = new PmsProductCategory();
        wrapper.in("id",ids);
        category.setShowStatus(showStatus);
        int update = productCategoryMapper.update(category, wrapper);
        return update;
    }

    //TODO 以层级形式获取商品分类
    @Override
    public List<PmsProductCategoryWithChildrenItem> listWithChildren() {
        List<PmsProductCategoryWithChildrenItem> items = productCategoryDao.listWithChildren();
        return items;
    }
}




